/**
 * createAuthor: jaden.liu
 * createTime: 2019-09-07
 * version: 1.0.0
 */

/* ------------------------------------------------------- */
var LQ = {
    /**
     * 获取表单属性值
     * @param selector
     */
    getFormValue: function (selector) {
        var formObj = {}, _this;
        $(selector + ' :input[name]').each(function (index, obj) {
            _this = $(obj);
            formObj[obj.name] = obj.value;
        });
        return formObj;
    },
    /**
     * 获取表单属性值序列化后的字符串
     * @param selector
     */
    getFormStr: function (selector) {
        var formObj = LQ.getFormValue(selector);
        return JSON.stringify(formObj);
    },
};
window['LQ'] = LQ;

/* ------------------------------------------------------- */
/**
 * 将form数据格式化成json对象
 */
$.fn.serializeJson = function () {
    var serializeObj = {};
    var array = this.serializeArray();
    // var str = this.serialize();
    $(array).each(function () {
        if (serializeObj[this.name]) {
            if ($.isArray(serializeObj[this.name])) {
                serializeObj[this.name].push(this.value);
            } else {
                serializeObj[this.name] = [serializeObj[this.name], this.value];
            }
        } else {
            serializeObj[this.name] = this.value;
        }
    });
    return serializeObj;
};

/**
 * 将form数据格式化成json字符串
 */
$.fn.stringifyJson = function () {
    var serializeObj = this.serializeJson();
    return JSON.stringify(serializeObj);
};

